export function ListNode(val, next) {
  this.val = val === undefined ? 0 : val
  this.next = next === undefined ? null : next
}

/**
 * 通过数组创建链表
 * @param {*} arr
 * @returns {ListNode}
 */
export function createListNode(arr, isLoop = false) {
  if (arr.length === 0) return null
  let head = new ListNode(arr[0])
  let cur = head
  for (let i = 1; i < arr.length; i++) {
    cur.next = new ListNode(arr[i])
    cur = cur.next
  }
  if (isLoop) cur.next = head
  return head
}

export function listNodeToList(node) {
  const list = []
  while (node) {
    list.push(node.val)
    node = node.next
  }
  return list
}
